/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aliang.jp.jp;

import java.awt.Cursor;
import java.awt.event.MouseEvent;

/**
 * 选择状态，表示当前用户可进行拖动选择抓图区
 * @author liuruile
 */
public class SelectedState extends State {

    @Override
    /**
     * 选择状态在鼠标按下时，记录选择的起点，并将鼠标置成“十”字形，表示可以进行拖选啦
     */
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            start = e.getPoint();
            parent.topFrame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
        }else{
            start = null;
        }
    }

    @Override
    /**
     * 对于鼠标的点击，在选择状态只处理右键双击，表示用户取消本次抓屏操作
     */
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON3) {
            parent.dispose();
        }
    }

    @Override
    /**
     * 释放鼠标时，记录结束点，并恢复默认鼠标状态，如果此时有选中区的话，当前状态将
     * 会由选择状态 －－跳转到－－＞ 修改状态
     */
    public void mouseReleased(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            end = e.getPoint();
            parent.topFrame.setCursor(Cursor.getDefaultCursor());
            if (getSelectedRect() != null)
            {
                changeState(this, State.getModifyState());
            }
        }
        else
        {
            end = null;
        }
    }

    @Override
    /**
     * 拖动时需要不断修改结束点，并对选择区进行重绘
     */
    public void mouseDragged(MouseEvent e) {
        end = e.getPoint();
        parent.rectPanel.repaint();
    }

    @Override
    /**
     * 选择状态时默认使用“十”作为默认鼠标状态
     */
    public void mouseMoved(MouseEvent e) {
        parent.topFrame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }
}
